<script setup lang="ts">
import CommonModel from "./CommonModel.vue";
import { inject, ref } from "vue";
import { popMessage, popMessageKey } from "@/type";
import { useStore } from '../stores/index'
import { storeToRefs } from 'pinia'
import axios from "../plugins/axiosInstance";
import { newMusicList, getMyMusicList } from '@api'
const popMessage = inject(popMessageKey) as popMessage;

/**
 * 待收藏的歌曲id 初始化未为 -1
 */
const store = useStore()
const newListTitle = ref('')
const myMusicList = storeToRefs(store).getMyMusicList
const CollectModel = storeToRefs(store).getCollectModel;

const collectSingle = (listId: string) => {
    if (store.CollectModel.allModel) {
        axios({
            url: "/musicList/collectAllMusic",
            method: "post",
            headers: {
                'Content-Type': 'application/json'
            },
            data: JSON.stringify(store.CollectModel.musicIds)
            ,
            params: {
                listId
            }
        }).then((res) => {
            if (res.data.code == 1) {
                popMessage("成功添加", 'success');
                store.getMyMusicList()
                store.closeCollectModel()
            }
        })
        return;
    }
    axios.post(`/musicList/addMusic?musicListId=${listId}&musicId=${CollectModel.value.musicId}`)
        .then(res => {
            if (res.data.code == 1) {
                popMessage("成功添加", 'success');
                getMyMusicList()
            } else {
                popMessage("该歌单已存在该歌曲", 'error')
            }
        })
}
const createNewMusicList = () => {
    newMusicList(newListTitle.value).then(res => {
        if (res.data.code == 1) {
            getMyMusicList()
            store.switchCollectNewList()
            popMessage("歌单创建成功", 'success')
        } else {
            popMessage("歌单创建失败", 'error')
        }
    })
}
</script>
<template>
    <RouterView name="Carousel" />
    <div class="main">
        <RouterView name="IndexPage" />
        <RouterView name="MusicList" />
        <RouterView name="Friends" />
        <RouterView name="PersonCenter" />
        <RouterView name="UserMusic" />
        <RouterView name="SingleMusic" />
        <RouterView name="Singer" />
        <RouterView name="Search" />
        <RouterView name="MoreMusicList" />
        <RouterView name="MoreMusic" />
        <RouterView name="PostPage" />
    </div>
    <CommonModel :is-open="CollectModel.show" :close="store.closeCollectModel"
        :title="CollectModel.newList ? '新歌单' : '添加到歌单'" :height="CollectModel.newList ? 200 : 414">
        <div v-show="!CollectModel.newList" class="addMusicToList">
            <div @click="store.switchCollectNewList" class="listItem" style="background-color: #e6e6e6;">
                <a class="container">
                    <img class="add" src="../assets/add-circle.svg" alt="添加">
                    <span style="font-size: 14px;">新歌单</span>
                </a>
            </div>
            <div class="listItem" v-for="list of myMusicList" @click="collectSingle(list.id as string)">
                <img class="listImage" src="../assets/image/miku.jpg" alt="歌单">
                <div class="listInfo">
                    <span style="font-size: 12px;">{{ list.title }}</span>
                    <span style="font-size: 12px;">{{ list.musicNum }}首</span>
                </div>
            </div>
        </div>
        <div v-show="CollectModel.newList" class="addMusicToList">
            <div class="newList">
                <div class="input">
                    <label for="newMusicList">歌单名: </label><input id="newMusicList" v-model="newListTitle" type="text"
                        placeholder="输入歌单名">
                </div>
                <div class="btns">
                    <div class="func">
                        <span @click="createNewMusicList">保存</span>
                        <span @click="store.switchCollectNewList">返回</span>
                    </div>
                </div>
            </div>
        </div>
    </CommonModel>
</template>
<style lang="css" scoped>
.func {
    margin-top: 25px;

}

.func span {
    background-color: #f6f6f6;
    padding: 6px 20px;
    margin-right: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    cursor: pointer;
}

#newMusicList {
    width: 320px;
    height: 20px;
}

.newList {
    display: flex;
    justify-content: center;
    height: 150px;
    align-items: center;
    flex-direction: column;
}

.main {
    width: var(--content-width);
    margin: auto;
    border: 1px solid #ccc;
    /* 不改变盒子大小 */
    box-sizing: border-box;
    margin-bottom: 150px;
}

.addMusicToList {
    overflow: scroll;
}

.addMusicToList .listItem:nth-child(n+2) {
    border-top: 1px solid #e0e0e0;
    display: flex;
}


.addMusicToList .listItem:nth-child(n+2):hover {
    background-color: #f2f2f2;
}


.addMusicToList .listItem {
    padding: 10px 0 10px 35px;
    cursor: pointer;
    border: 1px solid #e0e0e0;
    height: 36px;
}

.addMusicToList {
    background-color: #fff;
}

.container {
    display: inline-flex;
    align-items: center;
    padding-bottom: 1px;
}

.add {
    width: 38px;
    height: 38px;
    margin-right: 10px;
}

.listImage {
    height: 38px;
    width: 38px;
}

.listInfo {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: 38px;
    margin-left: 10px;
}
</style>